/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sharepoint3;

import com.microsoft.schemas.sharepoint.soap.Lists;
import com.sun.org.apache.xerces.internal.dom.ElementNSImpl;
import java.net.Authenticator;
import javax.xml.ws.BindingProvider;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 *
 * @author Thyago
 */
public class Sharepoint3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
        String userName = "";
        String password = "";
        String end = "http://www.fatecpg.com.br/fatec/_vti_bin/lists.asmx";
        com.microsoft.schemas.sharepoint.soap.ListsSoap port = null;
        com.microsoft.schemas.sharepoint.soap.Lists service = new Lists();
        port = service.getListsSoap();
        ((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName);
        ((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
        NtlmAuthenticator authenticator = new NtlmAuthenticator(userName, password);
        Authenticator.setDefault(authenticator);
        String listName = "Cursos";
        String rowLimit = "150";
        String viewName = "";
        com.microsoft.schemas.sharepoint.soap.GetListItems.ViewFields viewFields = null;
        com.microsoft.schemas.sharepoint.soap.GetListItems.Query query = null;
        com.microsoft.schemas.sharepoint.soap.GetListItems.QueryOptions queryOptions = null;
        String webID = "";
        com.microsoft.schemas.sharepoint.soap.GetListItemsResponse.GetListItemsResult result =  port.getListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, webID);
        
        
        
        
        Object listResult = result.getContent().get(0);
            if ((listResult != null) && (listResult instanceof ElementNSImpl)) {
                ElementNSImpl node = (ElementNSImpl) listResult;
 
                //Dumps the retrieved info in the console
                Document document = node.getOwnerDocument();
                System.out.println("SharePoint Online Lists Web Service Response:" + Manager.xmlToString(document));
 
//                //selects a list of nodes which have z:row elements
//                NodeList list = node.getElementsByTagName("z:row");
//                System.out.println("=> " + list.getLength() + " results from SharePoint Online");
// 
//                //Displaying every result received from SharePoint, with its ID
//                for (int i = 0; i < list.getLength(); i++) {
// 
//                    //Gets the attributes of the current row/element
//                    NamedNodeMap attributes = list.item(i).getAttributes();
//                    System.out.println("******** Item ID: " + attributes.getNamedItem("ows_ID").getNodeValue()+" ********");
// 
//                    //Displays all the attributes of the list item that correspond to the column names given
//                    for (String columnName : listColumnNames) {
//                        String internalColumnName = "ows_" + columnName;
//                        if (attributes.getNamedItem(internalColumnName) != null) {
//                            System.out.println(columnName + ": " + attributes.getNamedItem(internalColumnName).getNodeValue());
//                        } else {
//                            throw new Exception("Couldn't find the '" + columnName + "' column in the '" + listName + "' list in SharePoint.\n");
//                        }
//                    }
//                }
            } else {
                throw new Exception(listName + " list response from SharePoint is either null or corrupt\n");
            }
            
            
            
            
            
//        System.out.println(result.toString());
    } catch (Exception ex) {
        System.err.println(ex);
    }
    }       
}
